package com.jhgsys.internal.contractor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.contractor.entity.BaseContractor;
import com.jhgsys.internal.contractor.mapper.BaseContractorMapper;
import com.jhgsys.internal.contractor.service.IBaseContractorService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 承包方设置 Service实现
 *
 * @author admin
 * @date 2024-07-29 14:19:02
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BaseContractorServiceImpl extends ServiceImpl<BaseContractorMapper, BaseContractor> implements IBaseContractorService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-07-29 14:19:02
     * @param request QueryRequest
     * @param baseContractor baseContractor
     * @return IPage<BaseContractor>
    */
    @Override
    public IPage<BaseContractor> findBaseContractors(QueryRequest request, BaseContractor baseContractor) {
        LambdaQueryWrapper<BaseContractor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseContractor::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(baseContractor.getContractorName())){
            queryWrapper.like(BaseContractor::getContractorName,baseContractor.getContractorName());
        }
        if(StringUtils.isNotBlank(baseContractor.getTenantId())){
            queryWrapper.eq(BaseContractor::getTenantId,baseContractor.getTenantId());
        }
        if(StringUtils.isNotBlank(baseContractor.getStatus())){
            queryWrapper.eq(BaseContractor::getStatus,baseContractor.getStatus());
        }


        Page<BaseContractor> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-07-29 14:19:02
     * @param baseContractor baseContractor
     * @return List<BaseContractor>
     */
    @Override
    public List<BaseContractor> findBaseContractors(BaseContractor baseContractor) {
	    LambdaQueryWrapper<BaseContractor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseContractor::getDeletedFlg, '0');
        if(StringUtils.isNotBlank(baseContractor.getTenantId())){
            queryWrapper.eq(BaseContractor::getTenantId,baseContractor.getTenantId());
        }
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-07-29 14:19:02
    * @param contractorId
    * @return BaseContractor
    */
    @Override
    public BaseContractor findBaseContractorById(String contractorId){
        return this.baseMapper.selectById(contractorId);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-07-29 14:19:02
     * @param baseContractor baseContractor
     */
    @Override
    @Transactional
    public void createBaseContractor(BaseContractor baseContractor) {
        baseContractor.setCreatedTime(new Date());
        this.save(baseContractor);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-07-29 14:19:02
     * @param baseContractor baseContractor
     */
    @Override
    @Transactional
    public void updateBaseContractor(BaseContractor baseContractor) {
        baseContractor.setUpdatedTime(new Date());
        this.saveOrUpdate(baseContractor);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-07-29 14:19:02
     * @param baseContractor baseContractor
     * @param baseContractorIds baseContractorIds
     */
    @Override
    @Transactional
    public void deleteBaseContractor(BaseContractor baseContractor,String baseContractorIds) {
        List<String> list = Arrays.asList(baseContractorIds.split(StringPool.COMMA));
        this.baseMapper.update(baseContractor,new LambdaQueryWrapper<BaseContractor>().in(BaseContractor::getContractorId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-07-29 14:19:02
    * @param baseContractorIds baseContractorIds
    */
    @Override
    @Transactional
    public void physicallyDeleteBaseContractor(String baseContractorIds) {
        List<String> list = Arrays.asList(baseContractorIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseContractor> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(BaseContractor::getContractorId,list);
        this.remove(wrapper);
    }

    @Override
    public void updateBaseContractorDeletedFlg(BaseContractor contractor) {
        this.baseMapper.update(contractor,new LambdaUpdateWrapper<BaseContractor>()
                .eq(BaseContractor::getContractorId,contractor.getContractorId()));
    }


    @Override
    public BaseContractor findByName(String contractorName) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<BaseContractor>()
                .eq(BaseContractor::getContractorName,contractorName)
                .eq(BaseContractor::getDeletedFlg,FebsConstant.VALID)
                .last("limit 1"));
    }
}
